<?php
class SearchController extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->model ( 'object', '', TRUE );
		$this->load->model ( 'photo', '', TRUE );
		$this->load->model ( 'location', '', TRUE );
		$this->load->model ( 'type', '', TRUE );
	}
	function index() {
		
		$crrLatitude = $_POST ['latitude'];
		$crrLongtitude = $_POST ['longitude'];
		
		$id_Type = $_POST ['type'];
		$name = $_POST ['name'];
		$distance = $_POST ['distance'];
		if ($id_Type == 0)
			$id_Type = '';
			/*$crrLongtitude = 105.847349;
		$crrLatitude = 21.006237;
		
		$id_Type = '1';
		$name = '';
		$distance = '';
	*/
		if ($distance != '') {
			$result3 = $this->location->getDistance ( $crrLatitude, $crrLongtitude, $distance );
		} else
			$result3 = $this->location->getDistance ( $crrLatitude, $crrLongtitude, 1000 );
		if ($result3) {
			
			foreach ( $result3 as $row3 ) {
				$id_Location = $row3 ['id_Location'];
				
				$result2 = $this->object->search ( $id_Type, $name, $id_Location );
				
				if ($result2) {
					foreach ( $result2 as $row2 ) {
						//print_r($row3['id_Location'].' vs '.$row3['distance'].'<br>');
						$result [] = $row2;
					
					}
				}
			
			}
			
			print (json_encode ( $result )) ;
		}
	
	}
	function getDistance() {
	}
}